from mvvmQt.Parser import Parser
from mvvmQt.Observable import Observable, Computed
from mvvmQt.rewriteContorls import Window
from PyQt5.QtCore import pyqtSignal, QThread
import os

class Model:
    def __init__(self):
        self.pos = Observable('200, 200')
        self.spacing = Observable(10)
        self.spacing1 = Computed([self.spacing], lambda v: v[0] > 0)
        self.repeat = Observable(True)
        self.tipShow = Observable(False)
        self.selectIndex = Observable(2)
        self.isDown = Observable(False)
        self.isDown.subscribe(self.test)

    def test(self, v):
        print(123, v)

class Event:
    def __init__(self, models: Model):
        self.models = models

    def addSpacing(self, ele):
        self.models.spacing.value += 1

    def downSpacing(self, ele):
        if self.models.spacing.value > 0:
            self.models.spacing.value -= 1

    def showTips(self, ele):
        self.models.tipShow.value = not self.models.tipShow.value
        if self.models.tipShow.value:
            self.models.pos.value = '300, 300'
        else:
            self.models.pos.value = '200, 200'

    def change(self, v, ele):
        print(v)

    def testSignal(self, ele):
        w = p.idElements['mainWindow'].qt
        w.aSignal.emit(123)

class CParser(Parser):
    def build(self):
        super().build()
        w = p.idElements['mainWindow'].qt
        th = Thread(10, w)
        th.bSignal.connect(self.test)

        th2 = Thread(100, w)
        th2.bSignal.connect(self.test)
        th.start(QThread.Priority.HighestPriority)
        th2.start(QThread.Priority.LowestPriority)

    def test(self, i):
        print('bSignal emit', i)

class MainWindow(Window):
    aSignal = pyqtSignal(int)

class Thread(QThread):
    bSignal = pyqtSignal(int)
    def __init__(self, num, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.num = num
        self.isWorking = True

    def run(self):
        for i in range(100):
            print(self.num ** 2)

    def test(self, s):
        print('aSignal emit', s, self.priority())

if __name__ == "__main__":
    models = Model()
    events = Event(models)
    p = CParser(os.path.abspath("%s/../" % __file__), models=models, events=events)
    p.addQtClass('mainWindow', MainWindow)
    p.build()
    p.run()